﻿using System.Collections.Generic;
using System.Threading.Tasks;

namespace Sidvall.Web.IO
{
    public class StoreCollection : System.Collections.ObjectModel.KeyedCollection<string, Store>
    {
        #region Public Members

        #region Add

        public void Add(string storeId, string rootPath, string authorizationProviderFactory, string authorizationProviderFactoryContext)
        {
            var item = new Store()
            {
                StoreId = storeId,
                RootPath = rootPath,
                AuthorizationManagerFactory = authorizationProviderFactory,
                AuthorizationManagerFactoryContext = authorizationProviderFactoryContext,
            };
            Add(item);
        }

        #endregion
        #region AddRange

        public void AddRange(IEnumerable<Store> items)
        {
            if (items == null)
                return;
            foreach (var item in items)
            {
                if (item != null)
                    Add(item);
            }
        }

        #endregion
        #region GetKeyForItem

        protected override string GetKeyForItem(Store item)
        {
            if (item == null)
                throw new System.ArgumentNullException("item");
            return item.StoreId;
        }

        #endregion
        #region FindStore

        public Store FindStore(string storeId)
        {
            if (string.IsNullOrWhiteSpace(storeId))
                return null;
            if (Contains(storeId))
                return this[storeId];
            return null;
        }

        #endregion
        #region InitializeAsync

        public async Task InitializeAsync()
        {
            foreach (var item in this)
                await item.InitializeAsync().ConfigureAwait(false);
        }

        #endregion

        #endregion
        #region Constructors

        public StoreCollection()
            : base(System.StringComparer.InvariantCultureIgnoreCase)
        {
        }

        #endregion
    }
}
